﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'PRC_TB_ALERTS_SE')
	BEGIN
		DROP  Procedure  dbo.PRC_TB_ALERTS_SE
	END

GO

CREATE Procedure dbo.PRC_TB_ALERTS_SE
	(
		@TA_USER_ID int,
		@TA_STATE int = null,
		@TA_TYPE int = null,
		@CHECK_SLEEP int
	)

AS

select 
	TA_ID,
	TA_USER_ID,
	TA_NAME,
	TA_STATE,
	TA_SLEEP_DATE,
	TA_TYPE
from tb_alerts
where ta_user_id = @ta_user_id
and (@TA_STATE is null or TA_STATE = @TA_STATE)
and (@TA_TYPE is null or TA_TYPE = @TA_TYPE)
and (@CHECK_SLEEP is null or @CHECK_SLEEP = 0 or (@CHECK_SLEEP = 1 and (TA_SLEEP_DATE is null or datediff(s,TA_SLEEP_DATE,getdate()) >= 0 )))
order by ta_name

GO

GRANT EXEC ON dbo.PRC_TB_ALERTS_SE TO PUBLIC

GO
